/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;

import Data.Enemigo.ManejoEnemigo;
import Data.Jugador;
import Data.Nivel;
import com.nilo.plaf.nimrod.NimRODLookAndFeel;
import com.nilo.plaf.nimrod.NimRODTheme;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import org.jfugue.Pattern;

/**
 *
 * @author REI
 */
public class Principal extends javax.swing.JFrame {

    /**
     * Creates new form Principal
     */
    EditorTexto tab;
    String salida;
    ArrayList<Nivel> niveles;
    File archivoaux;
    File archivonivel;
    File archivoenemigo;
    File archivojugador;
    ArrayList<ManejoEnemigo> enemigos;
    Jugador jugador;
    Compilacion compilar;
    DefaultListModel modelonivel;
    DefaultListModel modeloenemigo;
    
    public Principal() {
        
        setDefaultLookAndFeelDecorated(true);
        setBackground(Color.WHITE);
        try {

            NimRODTheme tema = new NimRODTheme("src/Temas/NimRODThemeFile1.theme");
            NimRODLookAndFeel milook = new NimRODLookAndFeel();

            NimRODLookAndFeel.setCurrentTheme(tema);
            UIManager.setLookAndFeel(milook);



        }catch (Exception e) {
            System.out.println(e);
        }
        initComponents();
        compilar = new Compilacion();
        niveles = new ArrayList<Nivel>();
        enemigos = new ArrayList<ManejoEnemigo>();
        modelonivel = new DefaultListModel();
        listaniveles.setModel(modelonivel);
        modeloenemigo = new DefaultListModel();
        listaenemigos.setModel(modeloenemigo);
    }
    
    public void AbrirArchivos()
    {
        boolean cargado = false;
        String[] patharchivo = null;
        JFileChooser choosercargar = new JFileChooser();
        choosercargar.setFileFilter(new Filtro("psj", "archivo *.psj"));
        choosercargar.setFileFilter(new Filtro("lvl", "archivo *.lvl"));
        choosercargar.setFileFilter(new Filtro("bad", "archivo *.bad"));


        choosercargar.showOpenDialog(this);


        if (choosercargar.getSelectedFile() != null) {
            salida = choosercargar.getSelectedFile().getAbsolutePath();
            File f = new File(salida);
            if (!f.exists()) {


                JOptionPane.showMessageDialog(null, "No Selecciono un Archivo");    //No existe
                } else {
                cargado = true;
                patharchivo = new String[2];
                patharchivo[0]=f.getName();
                patharchivo[1]=salida;
            }
        }// final del if

        if (cargado == true) {

            try {


                if (patharchivo != null && estaAbierto(patharchivo) ) {
                File archivo = null;
                BufferedReader entrada = null;
                String lectura = "";
                String contenido = "";
                archivo = new File(patharchivo[1].trim());
                contenido = "";
                if (archivo.getName().toLowerCase().endsWith(".lvl")||archivo.getName().toLowerCase().endsWith(".bad")||archivo.getName().toLowerCase().endsWith(".psj")) {
                    try {
                        entrada = new BufferedReader(new FileReader(archivo));
                    } catch (FileNotFoundException ex) {
                        System.out.println("no se puede leer el archivo");
                    }
                    try {
                        lectura = entrada.readLine();
                        while (lectura != null) {
                            contenido = contenido + lectura + "\n";
                            lectura = entrada.readLine();
                        }
                    } catch (IOException ex) {
                        System.out.println("fallo el buffer de lectura");
                    }


                      tab  = new EditorTexto();
                      tab.Inicializar(patharchivo[0],patharchivo[1], contenido);
                      Tabbs.add(patharchivo[0], tab);
                      Tabbs.setSelectedIndex(Tabbs.getTabCount()-1);


                }

            }




            }catch (Exception ex) {
                ex.printStackTrace();

        }


        } else {

            JOptionPane.showMessageDialog(null, "Falta que abran un archivo");
        }
    }
    
    public boolean estaAbierto(String[] path) {

        EditorTexto temp;
        String paths = "";
        for (int i = 0; i < Tabbs.getTabCount(); i++) {
            temp = (EditorTexto ) Tabbs.getComponentAt(i);
            paths = temp.getPath();
            if (paths.compareTo(path[1]) == 0) {
                Tabbs.setSelectedIndex(i);
                return false;
            }
        }
        return true;
    }
    
    public void GuardarArchivo(String path, String contenido) {
        File archivo = null;
        BufferedWriter escribir = null;

        try {
            archivo = new File(path);
            escribir = new BufferedWriter(new FileWriter(archivo));

            if (contenido != null) {
                escribir.write(contenido);

                }
            escribir.close();
        } catch (IOException ex) {
        }
    }
    
    public void Guardar()
    {
        EditorTexto aux = (EditorTexto)Tabbs.getComponentAt(Tabbs.getSelectedIndex());

         if(aux!=null)
         {
            GuardarArchivo(aux.getPath(), aux.getContenido());
         }
    }
    
    public void Copiar()
    {
        EditorTexto aux = (EditorTexto)Tabbs.getComponentAt(Tabbs.getSelectedIndex());

         if(aux!=null)
         {
            aux.copiar();
         }
    }
    
    public void Pegar()
    {
        EditorTexto aux = (EditorTexto)Tabbs.getComponentAt(Tabbs.getSelectedIndex());

         if(aux!=null)
         {
            aux.pegar();
         }
    }
    
    public void GuardarComo()
    {
        
        String miarchivo;
        String ext;
        JFileChooser choosercargar = new JFileChooser();
        choosercargar.setFileFilter(new Filtro("txt", "archivo *.psj"));
        choosercargar.setFileFilter(new Filtro("lvl", "archivo *.lvl"));
        choosercargar.setFileFilter(new Filtro("bad", "archivo *.bad"));
        

        choosercargar.showSaveDialog(this);
        
        if (choosercargar.getSelectedFile() != null) {
            miarchivo = choosercargar.getSelectedFile().getAbsolutePath();

            ext = choosercargar.getFileFilter().getDescription();
            ext = ext.substring(ext.length()-4, ext.length());
            
            
            if(Tabbs.getComponentCount()!=0)
            {
                 EditorTexto aux = (EditorTexto)Tabbs.getComponentAt(Tabbs.getSelectedIndex());
                 if(aux!=null)
                 {
                    GuardarArchivo(miarchivo+ext, aux.getContenido());
                 }
            }
            
             
            
            
        }
        else
        {
             JOptionPane.showMessageDialog(null, "No Selecciono un Archivo");    //No existe
        }
        
            
    }
    
    public File Abrir(String extension)
    {
        File archivo = null;
        String path;
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new Filtro(extension,"archivo *."+extension));
        chooser.showOpenDialog(this);
        
        if(chooser.getSelectedFile()!=null)
        {
            path = chooser.getSelectedFile().getAbsolutePath();
            archivo = new File(path);
            if(archivo.exists())
            {
                return archivo;
            }
            else
            {
                return archivo;
            }
        }
        
        return archivo;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btabrir = new javax.swing.JButton();
        btguardar = new javax.swing.JButton();
        btguardarcomo = new javax.swing.JButton();
        btcopiar = new javax.swing.JButton();
        btpegar = new javax.swing.JButton();
        Tabbs = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txniveles = new javax.swing.JTextField();
        btniveles = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaniveles = new javax.swing.JList();
        btquitarniveles = new javax.swing.JButton();
        btaddniveles = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txenemigos = new javax.swing.JTextField();
        btenemigos = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaenemigos = new javax.swing.JList();
        btquitarenemigos = new javax.swing.JButton();
        btaddenemigos = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txjugador = new javax.swing.JTextField();
        btjugador = new javax.swing.JButton();
        btaddjugador = new javax.swing.JButton();
        txarchjug = new javax.swing.JTextField();
        btiniciar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setRollover(true);

        btabrir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/open.png"))); // NOI18N
        btabrir.setToolTipText("Abrir");
        btabrir.setFocusable(false);
        btabrir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btabrir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btabrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btabrirActionPerformed(evt);
            }
        });
        jToolBar1.add(btabrir);

        btguardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/save.png"))); // NOI18N
        btguardar.setToolTipText("guardar");
        btguardar.setFocusable(false);
        btguardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btguardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btguardarActionPerformed(evt);
            }
        });
        jToolBar1.add(btguardar);

        btguardarcomo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/filesaveas.png"))); // NOI18N
        btguardarcomo.setToolTipText("Guardar Como..");
        btguardarcomo.setFocusable(false);
        btguardarcomo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btguardarcomo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btguardarcomo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btguardarcomoActionPerformed(evt);
            }
        });
        jToolBar1.add(btguardarcomo);

        btcopiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/copy.png"))); // NOI18N
        btcopiar.setToolTipText("Copiar");
        btcopiar.setFocusable(false);
        btcopiar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btcopiar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btcopiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcopiarActionPerformed(evt);
            }
        });
        jToolBar1.add(btcopiar);

        btpegar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/paste.png"))); // NOI18N
        btpegar.setToolTipText("Pegar");
        btpegar.setFocusable(false);
        btpegar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btpegar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btpegar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btpegarActionPerformed(evt);
            }
        });
        jToolBar1.add(btpegar);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 723, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(Tabbs, javax.swing.GroupLayout.DEFAULT_SIZE, 723, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Tabbs, javax.swing.GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Editor Texto", jPanel2);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.LEADING, javax.swing.border.TitledBorder.DEFAULT_POSITION), "Niveles", javax.swing.border.TitledBorder.LEADING, javax.swing.border.TitledBorder.TOP));

        jLabel1.setText("Niveles:");

        txniveles.setEditable(false);

        btniveles.setText("...");
        btniveles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnivelesActionPerformed(evt);
            }
        });

        listaniveles.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(listaniveles);

        btquitarniveles.setText("x");
        btquitarniveles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btquitarnivelesActionPerformed(evt);
            }
        });

        btaddniveles.setText("+");
        btaddniveles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btaddnivelesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txniveles, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btniveles, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btquitarniveles, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btaddniveles, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txniveles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btniveles))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btaddniveles)
                        .addGap(12, 12, 12)
                        .addComponent(btquitarniveles)))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.LEADING, javax.swing.border.TitledBorder.DEFAULT_POSITION), "Enemigos"));

        jLabel2.setText("Enemigos");

        txenemigos.setEditable(false);

        btenemigos.setText("...");
        btenemigos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btenemigosActionPerformed(evt);
            }
        });

        listaenemigos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(listaenemigos);

        btquitarenemigos.setText("x");
        btquitarenemigos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btquitarenemigosActionPerformed(evt);
            }
        });

        btaddenemigos.setText("+");
        btaddenemigos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btaddenemigosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txenemigos, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btenemigos, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btquitarenemigos, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btaddenemigos, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(60, 60, 60))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txenemigos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btenemigos))
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(btaddenemigos)
                        .addGap(11, 11, 11)
                        .addComponent(btquitarenemigos)))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Jugador", javax.swing.border.TitledBorder.LEADING, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel3.setText("Jugador:");

        txjugador.setEditable(false);

        btjugador.setText("...");
        btjugador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btjugadorActionPerformed(evt);
            }
        });

        btaddjugador.setText("+");
        btaddjugador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btaddjugadorActionPerformed(evt);
            }
        });

        txarchjug.setEditable(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(93, 93, 93)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txjugador, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                    .addComponent(txarchjug))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btaddjugador, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btjugador, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(232, Short.MAX_VALUE)))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txjugador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btjugador))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btaddjugador)
                    .addComponent(txarchjug, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addGap(42, 42, 42)
                    .addComponent(jLabel3)
                    .addContainerGap(44, Short.MAX_VALUE)))
        );

        btiniciar.setText("Iniciar Juego");
        btiniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btiniciarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(144, 144, 144)
                        .addComponent(btiniciar)))
                .addContainerGap(69, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(btiniciar)
                        .addContainerGap(115, Short.MAX_VALUE))))
        );

        jTabbedPane1.addTab("Configuracion de juego", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btabrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btabrirActionPerformed
        // TODO add your handling code here:
        AbrirArchivos();
    }//GEN-LAST:event_btabrirActionPerformed

    private void btguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btguardarActionPerformed
        // TODO add your handling code here:
        Guardar();
    }//GEN-LAST:event_btguardarActionPerformed

    private void btguardarcomoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btguardarcomoActionPerformed
        // TODO add your handling code here:
        GuardarComo();
    }//GEN-LAST:event_btguardarcomoActionPerformed

    private void btcopiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btcopiarActionPerformed
        // TODO add your handling code here:
        Copiar();
    }//GEN-LAST:event_btcopiarActionPerformed

    private void btpegarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btpegarActionPerformed
        // TODO add your handling code here:
        Pegar();
    }//GEN-LAST:event_btpegarActionPerformed

    private void btnivelesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnivelesActionPerformed
        // TODO add your handling code here:
        archivonivel = Abrir("lvl");
        
        if(archivonivel!=null)
        {
            txniveles.setText(archivonivel.getName());
        }
    }//GEN-LAST:event_btnivelesActionPerformed

    private void btenemigosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btenemigosActionPerformed
        // TODO add your handling code here:
        archivoenemigo = Abrir("bad");
        
        if(archivoenemigo!=null)
        {
            txenemigos.setText(archivoenemigo.getName());
        }
    }//GEN-LAST:event_btenemigosActionPerformed

    private void btjugadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btjugadorActionPerformed
        // TODO add your handling code here:
        archivojugador = Abrir("psj");
        
        if(archivojugador!=null)
        {
            txjugador.setText(archivojugador.getName());
        }
    }//GEN-LAST:event_btjugadorActionPerformed

    private void btaddnivelesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btaddnivelesActionPerformed
        // TODO add your handling code here:
        if(archivonivel!=null)
        {
        
            Nivel nivel = compilar.compilarNivel(archivonivel);

            if(nivel!=null)
            {
                if(nivel.isEspecial())
                {
                    JOptionPane.showMessageDialog(this,"El archivo seleccionado no puede ser un nivel");
                }
                else
                {
                     modelonivel.addElement(txniveles.getText());
                     niveles.add(nivel);
                     archivoaux = null;
                     txniveles.setText("");

                }
            }
        
        }
    }//GEN-LAST:event_btaddnivelesActionPerformed

    private void btquitarnivelesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btquitarnivelesActionPerformed
        // TODO add your handling code here:
        if(!niveles.isEmpty())
        {
            niveles.remove(listaniveles.getSelectedIndex());
            modelonivel.remove(listaniveles.getSelectedIndex());            
            System.out.println(listaniveles.getSelectedIndex());
        }
        
        
    }//GEN-LAST:event_btquitarnivelesActionPerformed

    private void btaddjugadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btaddjugadorActionPerformed
        // TODO add your handling code here:
        if(archivojugador != null)
        {
            jugador = compilar.compilarJugador(archivojugador);
            txarchjug.setText(txjugador.getText());
            txjugador.setText("");
        }
    }//GEN-LAST:event_btaddjugadorActionPerformed

    private void btiniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btiniciarActionPerformed
        // TODO add your handling code here:
        
        if(!niveles.isEmpty() && jugador != null && !enemigos.isEmpty())
        {
            Tablero  tablero= new Tablero(jugador, niveles,enemigos);
            Juego juego = new Juego(tablero);
            juego.setVisible(true);
            this.setVisible(false);
            
        }
    }//GEN-LAST:event_btiniciarActionPerformed

    private void btaddenemigosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btaddenemigosActionPerformed
        // TODO add your handling code here:
        
        if(archivoenemigo!=null)
        {

            ManejoEnemigo enemy = compilar.compilarEnemigo(archivoenemigo);

            if(enemy!=null)
            {


                     modeloenemigo.addElement(txenemigos.getText());
                     enemy.setId(modeloenemigo.size()-1);
                     enemigos.add(enemy);
                     archivoaux = null;
                     txenemigos.setText("");


            }
        
        }
    }//GEN-LAST:event_btaddenemigosActionPerformed

    private void btquitarenemigosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btquitarenemigosActionPerformed
        // TODO add your handling code here:
        if(!enemigos.isEmpty())
        {
            enemigos.remove(listaenemigos.getSelectedIndex());
            modeloenemigo.remove(listaenemigos.getSelectedIndex());            
            System.out.println(listaenemigos.getSelectedIndex());
        }
    }//GEN-LAST:event_btquitarenemigosActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane Tabbs;
    private javax.swing.JButton btabrir;
    private javax.swing.JButton btaddenemigos;
    private javax.swing.JButton btaddjugador;
    private javax.swing.JButton btaddniveles;
    private javax.swing.JButton btcopiar;
    private javax.swing.JButton btenemigos;
    private javax.swing.JButton btguardar;
    private javax.swing.JButton btguardarcomo;
    private javax.swing.JButton btiniciar;
    private javax.swing.JButton btjugador;
    private javax.swing.JButton btniveles;
    private javax.swing.JButton btpegar;
    private javax.swing.JButton btquitarenemigos;
    private javax.swing.JButton btquitarniveles;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JList listaenemigos;
    private javax.swing.JList listaniveles;
    private javax.swing.JTextField txarchjug;
    private javax.swing.JTextField txenemigos;
    private javax.swing.JTextField txjugador;
    private javax.swing.JTextField txniveles;
    // End of variables declaration//GEN-END:variables
}
